/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.makimo.fuzzy.sets.memberships;


/**
 *
 * @author Mateusz Papiernik
 */
public class TrapezoidMembershipFunction extends ContinuousMembershipFunction implements Integrable {
	public double a;
	public double b;
	public double c;
	public double d;
	
    public TrapezoidMembershipFunction(double a, double b, double c, double d) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
	}

    @Override
    public double integrate(double min, double max) {
    	return (c - b) * (d - a) * 0.5;
    }
    
	public Object value(Object obj) {
		double x = (Double)obj;
		
		if (x == b) return (double)1.0;
		if (x == d) return (double)0.0;
		
        if (x <= a) return (double)0.0;        
        if (a <= x && x <= b) return (double)((x-a)/(b-a));
        if (b <= x && x <= c) return (double)1.0;
        if (c <= x && x <= d) return (double)((d-x)/(d-c));
        
        return (double)0.0;
    }

	@Override
	public double min(double min) {
		return min > a ? min : a;
	}

	@Override
	public double max(double max) {
		return max < d ? max : d;
	}
}
